#!/bin/bash
set -e

TESTS=${TESTS:-""}

cd "$(dirname "$0")/.."

args=("$@")

echo "Running tests ${TESTS}"

if ! git diff --name-only | grep -Ev '(.git|.trash-cache|vendor|bin|k8s)' | grep -E '.*\.go' ; then
  PACKAGES="$(find . -name '*.go' -exec dirname {} \; | sort -u | grep -Ev '(.git|.trash-cache|vendor|bin|k8s)')"
else
  PACKAGES="$(git diff --name-only | grep -E '.*\.go' | xargs -I{} dirname ./{} | sort -u | grep -Ev '(.git|.trash-cache|vendor|bin|k8s)')"
fi

echo Packages: "${PACKAGES}"

[ "${ARCH}" == "amd64" ] && RACE=-race
if [ -n "${TESTS}" ]; then
  go test -v "${RACE}" "${args[@]}" ${PACKAGES} -check.f "${TESTS}" -coverprofile=coverage.out
else
  go test -v "${RACE}" "${args[@]}" ${PACKAGES} -coverprofile=coverage.out
fi
